package com.minhua.jijiehao2.mainframe.loan.loanfragment.impl

import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import android.util.Log
import android.widget.CompoundButton
import com.bumptech.glide.Glide
import com.minhua.jijiehao2.R
import com.minhua.jijiehao2.base.BaseFragment
import com.minhua.jijiehao2.base.adapter.ViewHolder
import com.minhua.jijiehao2.base.adapter.recyclerview.BaseCommonAdapter
import com.minhua.jijiehao2.bean.ChannelBean
import com.minhua.jijiehao2.mainframe.common.webviewactivity.impl.WebActivity
import com.minhua.jijiehao2.mainframe.launcher.loginactivity.impl.LoginActivity
import com.minhua.jijiehao2.mainframe.loan.loanfragment.imp.ILoanView
import com.minhua.jijiehao2.view.LoanGridView
import kotlinx.android.synthetic.main.fragment_loan.*


@Suppress("DEPRECATION")
class LoanFragment : BaseFragment<ILoanView, LoanPresenter>(), ILoanView {
    override fun getLayoutId(): Int = R.layout.fragment_loan

    override fun getPresenter(): LoanPresenter = LoanPresenter()

    companion object {
        fun newInstance(): LoanFragment {
            val fragment = LoanFragment()
            val args = Bundle()
            fragment.arguments = args
            return fragment
        }
    }

    private var moneyGridView: LoanGridView? = null
    private var dateGridView: LoanGridView? = null


    private val mOnCheckedChangeListener = CompoundButton.OnCheckedChangeListener { compoundButton, isChecked ->
        when (compoundButton.id) {
            R.id.cbMoney -> {
                if (isChecked) {
                    rlMoney.setBackgroundResource(R.mipmap.redbg)
                    cbDate.isChecked = false
                    if (moneyGridView == null) {
                        moneyGridView = LoanGridView(activity, mPresenter.getMoneyList())
                        moneyGridView!!.setOnDismissListener { cbMoney.isChecked = false }
                        moneyGridView!!.setLimitListener { position ->
                            moneyIndex = position
                            mPresenter.getListChannel()
                        }
                    }
                    if (!moneyGridView?.isShowing!!) {
                        moneyGridView?.showAsDropDown(llMenu)
                    }
                } else {
                    rlMoney.setBackgroundColor(resources.getColor(R.color.mainWhite))
                }
            }
            R.id.cbDate -> {
                if (isChecked) {
                    cbMoney.isChecked = false
                    rlDate.setBackgroundResource(R.mipmap.redbg)
                    if (dateGridView == null) {
                        dateGridView = LoanGridView(activity, mPresenter.getDateList())
                        dateGridView!!.setOnDismissListener { cbDate.isChecked = false }
                        dateGridView!!.setLimitListener { position ->
                            dateIndex = position
                            mPresenter.getListChannel()
                        }
                    }
                    if (!dateGridView?.isShowing!!) {
                        dateGridView?.showAsDropDown(llMenu)
                    }
                } else {
                    rlDate.setBackgroundColor(resources.getColor(R.color.mainWhite))
                }
            }
        }
    }

    private var adapter: BaseCommonAdapter<ChannelBean>? = null
    private val listChannel = arrayListOf<ChannelBean>()

    private var lastItemPosition: Int = 0
    private var gridLayoutManager: GridLayoutManager? = null

    override fun initView() {
        cbMoney.setOnCheckedChangeListener(mOnCheckedChangeListener)
        cbDate.setOnCheckedChangeListener(mOnCheckedChangeListener)

        adapter = object : BaseCommonAdapter<ChannelBean>(activity, R.layout.item_loan, listChannel) {
            override fun convert(holder: ViewHolder?, t: ChannelBean?) {
                holder?.setText(R.id.tvName, t?.pname ?: "")
                holder?.setText(R.id.tvCount, (t?.pBorrowNum ?: 0).toString())
                Glide.with(activity).load(t?.picUrl ?: "").into(holder?.getView(R.id.ivIcon))
                holder?.setOnClickListener(R.id.llItemView, {
                    //                    ChannelDetailActivity.intentStart(activity, t!!)
                    if(LoginActivity.logged(activity)){
                        WebActivity.intentStart(activity, t?.phttp!!, t.pname)
                    }
                })
            }
        }
        gridLayoutManager = GridLayoutManager(activity, 2, GridLayoutManager.VERTICAL, false)
        rvContent.layoutManager = gridLayoutManager
        rvContent.adapter = adapter

        refresh.setOnRefreshListener { mPresenter.getListChannel() }

        rvContent.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                val findLastCompletelyVisibleItemPosition = gridLayoutManager?.findLastCompletelyVisibleItemPosition()
                Log.e("CompletelyLast", findLastCompletelyVisibleItemPosition.toString())
                lastItemPosition = findLastCompletelyVisibleItemPosition!!
            }

            override fun onScrollStateChanged(recyclerView: RecyclerView?, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastItemPosition != 0 && lastItemPosition + 1 == adapter?.itemCount) {
                    mPresenter.loadMoreListChannel()
                }
            }
        })
    }

    override fun initData() {
        mPresenter.getListChannel()
    }

    private var moneyIndex = 0
    private var dateIndex = 0

    override fun getMoneyIndex(): Int = moneyIndex

    override fun getDateIndex(): Int = dateIndex

    override fun onChannelSuccess(list: List<ChannelBean>, needClear: Boolean) {
        refresh.isRefreshing = false
        if (needClear) {
            listChannel.clear()
        }
        listChannel.addAll(list)
        adapter?.notifyDataSetChanged()
    }
}
